# Maintainer: Santiago Torres-Arias <santiago@archlinux.org>
# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>

pkgname=cosign
pkgver=2.0.1
pkgrel=1
pkgdesc="Container Signing with support for ephemeral keys and Sigstore signing"
arch=('loong64' 'x86_64')
url="https://github.com/sigstore/cosign"
license=('Apache')
depends=('pcsclite')
makedepends=('go' 'git')
checkdepends=('go-tools')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/sigstore/${pkgname}/archive/v${pkgver}.tar.gz")
sha512sums=('2d8ce3c495818091e8077503fd2266bd0f3d8ee8a76f2a9b27ab1ab1cf1e2a01fee3ce6138f54d634015010d80e8193f79a8029dd3eb7a09c4b457be16ee4e50')
b2sums=('eb552f0160473cd98d4c876c126f1bbacb53deeddaa9b475f12c3baa5b076fdc06ac3e9783f785059a96baf3643d010617efb695fb6a10f28f5d40023fa6602d')
options=('!lto')

build(){
  cd "${pkgname}-${pkgver}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_ENABLED=1
  export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-buildid='' -ldflags=-X=sigs.k8s.io/release-utils/version.gitVersion=v$pkgver"
  mkdir build
  go build -o build/ ./cmd/cosign
  go build -o build/ ./cmd/sget
}

# Disabling tests, because of a segfault
#check() {
#  cd "${pkgname}-${pkgver}"
#  GODEBUG=x509sha1=1 make test # we have to allow sha1 certificates for tests
#}

package() {
  cd "${pkgname}-${pkgver}"
  install -Dm755 build/cosign "${pkgdir}/usr/bin/${pkgname}"

  # build bash completions
  mkdir -p "${pkgdir}/usr/share/bash-completion/completions"
  "$pkgdir"/usr/bin/cosign completion bash > "${pkgdir}/usr/share/bash-completion/completions/cosign"

  # build zsh completions
  mkdir -p "${pkgdir}/usr/share/zsh/site-functions"
  "$pkgdir"/usr/bin/cosign completion zsh > "${pkgdir}/usr/share/zsh/site-functions/_cosign"

  # build fish completions
  mkdir -p "${pkgdir}/usr/share/fish/vendor_completions.d/"
  "$pkgdir"/usr/bin/cosign completion fish > "${pkgdir}/usr/share/fish/vendor_completions.d/cosign.fish"

  install -Dm755 build/sget "${pkgdir}/usr/bin/sget"

  # build bash completions
  mkdir -p "${pkgdir}/usr/share/bash-completion/completions"
  "$pkgdir"/usr/bin/sget completion bash > "${pkgdir}/usr/share/bash-completion/completions/sget"

  # build zsh completions
  mkdir -p "${pkgdir}/usr/share/zsh/site-functions"
  "$pkgdir"/usr/bin/sget completion zsh > "${pkgdir}/usr/share/zsh/site-functions/_sget"

  # build fish completions
  mkdir -p "${pkgdir}/usr/share/fish/vendor_completions.d/"
  "$pkgdir"/usr/bin/sget completion fish > "${pkgdir}/usr/share/fish/vendor_completions.d/sget.fish"
}

